<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            width: 300px;
            height: 200px;
            background-color: pink;
        }

        span {
            position: absolute;
            top: 300px;
            width: 100px;
            height: 200px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>动画原理</div>
    <span>span</span>
    <button>点击span</button>
    <script>

        function animte(obj, target) {
            clearInterval(obj.timer)//每点击一次事件就清除上一次的定时器 达到不会因为重复点击造成的定时器加速
            obj.timer = setInterval(function () {//使用对象属性名来区分各个元素的定时器 也同时节省内存
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer)
                }
                obj.style.left = obj.offsetLeft + 1 + 'px'


            }, 30)
        }
        var div = document.querySelector('div')
        var span = document.querySelector('span')
        var btn = document.querySelector('button')
        animte(div, 300)
        btn.addEventListener('click', function () {
            animte(span, 200)

        })







    </script>
</body>

</html>